var urls = "http://localhost:8888/yihuan/";
var jurID = 3;

var imgObjTrue_0 = $("<img src='login/img/true.jpg' height=\"22\" width=\"23\">");
var imgObjFalse_0 = $("<img src='login/img/false.jpg' height=\"22\" width=\"23\">");
//用户昵称
var flas0 = false; //标识符
$("#userNickname").blur(function () {
    //取出用户昵称
    var nickname = $("#userNickname").val();
    //	用户昵称4-16位
    var usernameRegex = /^([a-zA-Z0-9_\u4e00-\u9fa5]{4,16})$/;
    //alert(varreg.test(name))
    if (nickname == null || nickname == "") {
        $("#recycle_0").empty();
        $("#recycle_0").append(imgObjFalse_0);
        $("#userNickname").val("");
        flas0 = false;
    } else if (!usernameRegex.test(nickname)) {
        $("#recycle_0").empty();
        $("#recycle_0").append(imgObjFalse_0);
        $("#userNickname").val("");
        flas0 = false;
    } else {
        $("#recycle_0").empty();
        $("#recycle_0").append(imgObjTrue_0);
        flas0 = true;
        return;
    }
});
var imgObjTrue_1 = $("<img src='login/img/true.jpg' height=\"22\" width=\"23\">");
var imgObjFalse_1 = $("<img src='login/img/false.jpg' height=\"22\" width=\"23\">");
//账号验证
var flas = false; //标识符
$("#userLoginName").blur(function () {
    //取出账号值
    var name = $("#userLoginName").val();
    //		账号必须为字母+数字的组合，不能出现特殊字符
    var varreg = /^(?=.*[a-zA-Z]+)(?=.*[0-9]+)[a-zA-Z0-9]+$/;
    //alert(varreg.test(name))
    if (name == null || name == "") {
        $("#recycle_1").empty();
        $("#recycle_1").append(imgObjFalse_1);
        $("#userLoginName").val("");
        flas = false;
    } else if (!varreg.test(name)) {
        $("#recycle_1").empty();
        $("#recycle_1").append(imgObjFalse_1);
        $("#userLoginName").val("");
        flas = false;
    } else {
        $("#recycle_1").empty();
        $("#recycle_1").append(imgObjTrue_1);
        findByLoginName();
        flas = true;
        return;
    }
});
var imgObjTrue_2 = $("<img src='login/img/true.jpg' height=\"22\" width=\"23\">");
var imgObjFalse_2 = $("<img src='login/img/false.jpg' height=\"22\" width=\"23\">");
//密码验证
var flas1 = false;
$("#userPassword").blur(function () {
    var pass = $("#userPassword").val(); //
    //密码规则为字母+数字的组合，不得出现特殊字符
    var patrm1 = /^[a-z0-9]{6,10}$/;
    if (pass == null || pass == "") {
        $("#recycle_2").empty();
        $("#recycle_2").append(imgObjFalse_2);
        $("#userPassword").val("");
        flas1 = false;
    } else if (!patrm1.test(pass)) {
        $("#recycle_2").empty();
        $("#recycle_2").append(imgObjFalse_2);
        $("#userPassword").val("");
        flas1 = false;
    } else {
        $("#recycle_2").empty();
        $("#recycle_2").append(imgObjTrue_2);
        flas1 = true;
        return;
    }
});
var imgObjTrue_3 = $("<img src='login/img/true.jpg' height=\"22\" width=\"23\">");
var imgObjFalse_3 = $("<img src='login/img/false.jpg' height=\"22\" width=\"23\">");
//重复密码验证
var flas2 = false;
$("#userPassword_1").blur(function () {
    var pass2 = $("#userPassword_1").val();
    var pass1 = $("#userPassword").val();
    if (pass2 == null || pass2 == "") {
        $("#recycle_3").empty();
        $("#recycle_3").append(imgObjFalse_3);
        $("#userPassword_1").val("");
        flas2 = false;
    } else if (pass2 != pass1) {
        $("#recycle_3").empty();
        $("#recycle_3").append(imgObjFalse_3);
        $("#userPassword_1").val("");
        flas2 = false;
    } else {
        $("#recycle_3").empty();
        $("#recycle_3").append(imgObjTrue_3);
        flas2 = true;
        return;
    }

});
var imgObjTrue_4 = $("<img src='login/img/true.jpg' height=\"22\" width=\"23\">");
var imgObjFalse_4 = $("<img src='login/img/false.jpg' height=\"22\" width=\"23\">");
//用户姓名验证
var flas3 = false;
$("#userName").blur(function () {
    var nc = $("#userName").val(); //
    var pattern = /^[\u4E00-\u9FA5]{2,6}$/;
    if (nc == null || nc == "") {
        $("#recycle_4").empty();
        $("#recycle_4").append(imgObjFalse_4);
        $("#userName").val("");
        flas3 = false;
    } else if (!pattern.test(nc)) {
        $("#recycle_4").empty();
        $("#recycle_4").append(imgObjFalse_4);
        $("#userName").val("");
        flas3 = false;
    } else {
        $("#recycle_4").empty();
        $("#recycle_4").append(imgObjTrue_4);
        flas3 = true;
        return;
    }


});
var imgObjTrue_5 = $("<img src='login/img/true.jpg' height=\"22\" width=\"23\">");
var imgObjFalse_5 = $("<img src='login/img/false.jpg' height=\"22\" width=\"23\">");
//用户性别
var flas4 = true;
$("#userSex").blur(function () {
    var sex = $("#userSex").val(); //
    if (sex == null || sex == "") {
        $("#recycle_5").empty();
        $("#recycle_5").append(imgObjFalse_5);
        flas4 = false;
    } else {
        $("#recycle_5").empty();
        $("#recycle_5").append(imgObjTrue_5);
        flas4 = true;
        return;
    }
});
var imgObjTrue_6 = $("<img src='login/img/true.jpg' height=\"22\" width=\"23\">");
var imgObjFalse_6 = $("<img src='login/img/false.jpg' height=\"22\" width=\"23\">");
//用户身份证号
var flas5 = false;
$("#userIDNumber").blur(function () {
    var sfz = $("#userIDNumber").val(); //
    //判断是身份证正则表达式
    var aa = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;

    if (sfz == null || sfz == "") {
        $("#recycle_6").empty();
        $("#recycle_6").append(imgObjFalse_6);
        $("#userIDNumber").val("");
        flas5 = false;

    } else if (!aa.test(sfz) || sfz.length != 18) {
        $("#recycle_6").empty();
        $("#recycle_6").append(imgObjFalse_6);
        $("#userIDNumber").val("");
        flas5 = false;
    } else {
        $("#recycle_6").empty();
        $("#recycle_6").append(imgObjTrue_6);
        flas5 = true;
        return;
    }
});
var imgObjTrue_7 = $("<img src='login/img/true.jpg' height=\"22\" width=\"23\">");
var imgObjFalse_7 = $("<img src='login/img/false.jpg' height=\"22\" width=\"23\">");
//邮箱验证
var flas6 = false;
$("#userEmail").blur(function () {
    var emil = $("#userEmail").val();
    //邮箱的正则表单达式
    var aa = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
    if (emil == null || emil == "") {
        $("#recycle_7").empty();
        $("#recycle_7").append(imgObjFalse_7);
        $("#userEmail").val("");
        flas6 = false;
    } else if (!aa.test(emil)) {
        $("#recycle_7").empty();
        $("#recycle_7").append(imgObjFalse_7);
        $("#userEmail").val("");
        flas6 = false;
    } else {
        $("#recycle_7").empty();
        $("#recycle_7").append(imgObjTrue_7);
        flas6 = true;
        return;
    }

});
var imgObjTrue_8 = $("<img src='login/img/true.jpg' height=\"22\" width=\"23\">");
var imgObjFalse_8 = $("<img src='login/img/false.jpg' height=\"22\" width=\"23\">");
//手机号验证
var flas7 = false;
$("#userPhone").blur(function () {
    var tell1 = $("#userPhone").val(); //
    //以1开头的正则表达式
    var aa = /^1\d{10}$/

    if (tell1 == null || tell1 == "") {
        $("#recycle_8").empty();
        $("#recycle_8").append(imgObjFalse_8);
        $("#userPhone").val("");
        flas7 = false;
    } else if (!aa.test(tell1) || tell1.length != 11) {
        $("#recycle_8").empty();
        $("#recycle_8").append(imgObjFalse_8);
        $("#userPhone").val("");
        flas7 = false;
    } else {
        $("#recycle_8").empty();
        $("#recycle_8").append(imgObjTrue_8);
        flas7 = true;
        return;
    }
});
var imgObjTrue_9 = $("<img src='login/img/true.jpg' height=\"22\" width=\"23\">");
var imgObjFalse_9 = $("<img src='login/img/false.jpg' height=\"22\" width=\"23\">");
//家庭地址
var flas8 = false;
$("#userAddress").blur(function () {
    var sellerAddress = $("#userAddress").val(); //
    if (sellerAddress == null || sellerAddress == "") {
        $("#recycle_9").empty();
        $("#recycle_9").append(imgObjFalse_9);
        $("#userAddress").val("");
        flas8 = false;
    } else {
        $("#recycle_9").empty();
        $("#recycle_9").append(imgObjTrue_9);
        flas8 = true;
        return;
    }
});

/*全国省份*/
function DoctorProvince() {
    $.ajax({
        url: "findByProvince",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        success: function (data) {
            if (data.code == 500) {

            } else {
                var str = null;
                for (var i = 0; i < data.ProvinceList.length; i++) {
                    str += "<option value='" + data.ProvinceList[i].provinceID + "'>" + data.ProvinceList[i].provinceName + "</option>";
                }
            }
            $("#Province").append(str);
        }
    });
}

/*全国县区*/
function changeCity() {
    //城市编号
    var cityID = $("#City").val();
    var DataSources = {
        "cityID": cityID
    };
    $.ajax({
        url: "findByCountry",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(DataSources),
        success: function (data) {
            if (data.code == 500) {
                window.location.href = urls + "404-error.html";
            } else {
                var str = null;
                for (var i = 0; i < data.CountryList.length; i++) {
                    str += "<option value='" + data.CountryList[i].countryID + "'>" + data.CountryList[i].countryName + "</option>";
                }
            }
            $("#Country").empty();
            $("#Country").append(str);
        }
    });
}

/*全国城市*/
function changeProvince() {
    //省份编号
    var provinceID = $("#Province").val();
    var DataSources = {
        "provinceID": provinceID
    };
    if (provinceID == "0") {
        $("#City").empty();
        $("#Country").empty();
        $("#City").append("<option value='0'>=====请选择城市=====</option>");
        $("#Country").append("<option value='0'>=====请选择县区=====</option>");
    } else {
        $.ajax({
            url: "findByCity",
            type: "POST",
            dataType: "json",
            contentType: 'application/json;charset=utf-8',
            data: JSON.stringify(DataSources),
            success: function (data) {
                if (data.code == 500) {
                    window.location.href = urls + "404-error.html";
                } else {
                    var str = null;
                    for (var i = 0; i < data.CityList.length; i++) {
                        str += "<option value='" + data.CityList[i].cityID + "'>" + data.CityList[i].cityName + "</option>";
                    }
                }
                $("#City").empty();
                $("#City").append(str);
                changeCity();
            }
        });
    }
}

// 登录名称查询
function findByLoginName() {
    var userLoginName = $("#userLoginName").val();  //用户账号
    var data = {
        "userLoginName": userLoginName
    };
    $.ajax({
        url: urls + "findUserByUserLoginNameData",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(data),
        async: false,
        success: function (data) {
            if (data.code == 200) {

            } else {
                Swal.fire("提示", "请重新输入账号", "error");
            }

        }
    });
}

//新增回收品类型信息
function add_Data() {
    //省份
    var Province = $("#Province option:selected").text();
    //城市
    var City = $("#City option:selected").text();
    //县区
    var Country = $("#Country option:selected").text();
    //详细地址
    var address = $("#userAddress").val();
    //个人地址
    var userAddress = Province + "省" + City + Country + address;
    var userNickname = $("#userNickname").val();
    var userLoginName = $("#userLoginName").val();
    var userPassword = $("#userPassword").val();
    var userName = $("#userName").val();
    var userSex = $("#userSex").val();
    var userIDNumber = $("#userIDNumber").val();
    var userPhone = $("#userPhone").val();
    var userEmail = $("#userEmail").val();
    var data = {
        "userAddress": userAddress,
        "userNickname": userNickname,
        "userLoginName": userLoginName,
        "userPassword": userPassword,
        "userName": userName,
        "userSex": userSex,
        "userIDNumber": userIDNumber,
        "userEmail": userEmail,
        "jurID": jurID,
        "userPhone": userPhone
    };
    if (flas0 == true && flas1 == true && flas2 == true && flas3 == true && flas4 == true && flas5 == true && flas6 == true && flas7 == true && flas8 == true && flas == true) {
        $.ajax({
            url: urls + "insertUserByUserData",
            type: "POST",
            dataType: "json",
            contentType: 'application/json;charset=utf-8',
            data: JSON.stringify(data),
            async: false,
            success: function (data) {
                if (data.code == 200) {
                    Swal.fire("提示", "新增数据成功！", "error");
                } else {
                    Swal.fire("提示", "新增数据失败", "error");
                }
            }
        });
    } else {
        Swal.fire("提示", "输入数据有误", "error");
    }
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index); //执行关闭
    parent.location.href = "/yihuan/Background-UserSellerList.html";//回到父级页面
}

/*加载数据*/
$(function () {
    DoctorProvince();
    $("#recycle_5").empty();
    $("#recycle_5").append(imgObjTrue_5);
    flas4 = true;
    $("#add_Data").click(function () {
        add_Data();
    });
});